package com.demon.framework.boot.autoconfigure.app;


import com.demon.framework.boot.context.SpringContext;
import com.demon.framework.boot.convert.DateConvert;
import com.demon.framework.common.aspect.LogAroundAop;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.boot.autoconfigure.condition.SearchStrategy;
import org.springframework.boot.autoconfigure.web.ServerProperties;
import org.springframework.boot.autoconfigure.web.servlet.error.ErrorMvcAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * APP 相关配置
 *
 * @author Demon-HY
 * @date 2020-7-23
 */
@Configuration
@AutoConfigureBefore(ErrorMvcAutoConfiguration.class)
public class AppAutoConfiguration {

    static {
        //兼容在header里面有json时报 The valid characters are defined in RFC 7230 and RFC 3986
        System.setProperty("tomcat.util.http.parser.HttpParser.requestTargetAllow", "|{}");
        System.setProperty("org.apache.tomcat.util.buf.UDecoder.ALLOW_ENCODED_SLASH", "true");
    }

    @Value("${server.tomcat.max-connections:${server.tomcat.maxConnections:800}}")
    private int tomcatMaxConnections;
    @Value("${server.tomcat.max-threads:${server.tomcat.maxThreads:500}}")
    private int tomcatMaxThreads;
    @Value("${server.tomcat.accept-count:${server.tomcat.acceptCount:200}}")
    private int tomcatAcceptCount;

    /**
     * 增大tomcat的并发数
     */
    @Bean
    @ConditionalOnWebApplication
    @ConditionalOnMissingBean(search = SearchStrategy.CURRENT)
    public ServerProperties serverProperties() {
        ServerProperties serverProperties = new ServerProperties();
        ServerProperties.Tomcat tomcat = serverProperties.getTomcat();

        //设置tomcat的参数
        tomcat.setMaxConnections(tomcatMaxConnections);
        tomcat.setMaxThreads(tomcatMaxThreads);
        tomcat.setAcceptCount(tomcatAcceptCount);
        return serverProperties;
    }

    /**
     * 注册MVC日志出入参切面
     */
    @Bean
    @ConditionalOnProperty(value = "demon.mvc.print.enabled")
    public LogAroundAop logAroundAop() {
        return new LogAroundAop();
    }

    /**
     * 自定义 数据转换器  日期
     */
    @Bean
    public DateConvert customDateFormat() {
        return new DateConvert();
    }

    @Bean
    public SpringContext springContext() {
        return new SpringContext();
    }

    /**
     * 健康检查,防止服务处于僵死状态
     */
    @Api("健康检测")
    @RestController
    @RequestMapping(value = "/health")
    public class HealthCheckApi {
        @ApiOperation(value = "健康检测", httpMethod = "GET")
        @RequestMapping(value = "/check", method = RequestMethod.GET)
        public String check() {
            return "i`m ok";
        }
    }
}
